package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;
import com.readystatesoftware.viewbadger.BadgeView;

public class SelecColumnListAdapter extends BaseAdapter {

	private Context context;
	private JSONArray array;
	private LayoutInflater inflater;
	private String url;
	String id;

	public SelecColumnListAdapter() {
	}

	public SelecColumnListAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.array = new JSONArray();
	}

	public JSONArray setDatas(JSONArray jsonArray) {
		this.array = jsonArray;
		this.notifyDataSetChanged();
		return this.array;
	}

	public JSONArray appendDatas(JSONArray jsonArray) {
		try {
			this.array = AppKit.joinJSONArray(this.array, jsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		this.notifyDataSetChanged();
		return this.array;
	}

	@Override
	public int getCount() {
		return array == null ? 0 : array.length();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.infox_activity_subscription_item, null);
			holder.selecColumnImg = (ImageView) convertView.findViewById(R.id.selecColumnImg);
			holder.selecColumnTitle = (TextView) convertView.findViewById(R.id.selecColumnTitle);
			holder.selecColumnIntroduction = (TextView) convertView.findViewById(R.id.selecColumnIntroduction);
			holder.selecColumnMessageCount = (TextView) convertView.findViewById(R.id.selecColumnMessageCount);
			holder.selecColumnSubscription = (TextView) convertView.findViewById(R.id.selecColumnSubscription);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		BadgeView surveyBadge = new BadgeView(context, holder.selecColumnImg);
		surveyBadge.setText("1");
		surveyBadge.setTextSize(11);
		surveyBadge.setGravity(Gravity.CENTER);
		surveyBadge.setBackgroundResource(R.drawable.infox_orange_roungbg);
		surveyBadge.setTextColor(Color.parseColor("#fa9600"));
		surveyBadge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);

		try {
			holder.selecColumnTitle.setText(array.getJSONObject(position).getString("title"));
			holder.selecColumnIntroduction.setText(array.getJSONObject(position).getString("memo"));
			url = array.getJSONObject(position).getString("image");
			id = array.getJSONObject(position).getString("id");
			int count = array.getJSONObject(position).getInt("count");
			if (count == 0) {
				surveyBadge.hide(false);
			} else {
				surveyBadge.setText(count + "");
				surveyBadge.show();
			}
			// DisplayImageOptions defaultOptions = new
			// DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_img_loadrong)
			// .showImageOnFail(R.drawable.infox_img_loadrong).showImageOnLoading(R.drawable.infox_img_loadrong).cacheInMemory(true)
			// .cacheOnDisc(true).build();
			// ImageLoader.getInstance().displayImage(url,
			// holder.selecColumnImg, defaultOptions);
			AppKit.SetNormalImg(url, holder.selecColumnImg);
			if (Boolean.valueOf(array.getJSONObject(position).getString("isSubscribes"))) {
				holder.selecColumnSubscription.setBackgroundResource(R.drawable.infox_searchitem_third);
				holder.selecColumnSubscription.setTextColor(Color.parseColor("#ffffff"));
				holder.selecColumnSubscription.setText("已订阅");
			} else {
				holder.selecColumnSubscription.setBackgroundResource(R.drawable.infox_orange_frame);
				holder.selecColumnSubscription.setTextColor(Color.parseColor("#fa6400"));
				holder.selecColumnSubscription.setText("订阅");
			}
			holder.selecColumnSubscription.setTag(R.id.tag_1, array.getJSONObject(position).getString("isSubscribes"));
			holder.selecColumnSubscription.setTag(R.id.tag_2, array.getJSONObject(position).getString("id"));
			holder.selecColumnSubscription.setTag(R.id.tag_3, array.getJSONObject(position).getString("type"));

		} catch (Exception e) {
			e.printStackTrace();
		}

		holder.selecColumnSubscription.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				final String state = (String) view.getTag(R.id.tag_1);
				String id = (String) view.getTag(R.id.tag_2);
				String type = (String) view.getTag(R.id.tag_3);
				String url = "Columnist".equalsIgnoreCase(type) ? "me/followColumnist" : "me/followEnterprise";
				Api.post(url, ApiParam.create().addParam("id", id).addParam("type", Boolean.valueOf(state) ? "2" : "1"), new ApiCallback(
						context, true) {

					@Override
					public void handleSuccess(ApiResult result) {
						if (result.getCode() == 100) {
							if (Boolean.valueOf(state)) {
								holder.selecColumnSubscription.setTag(R.id.tag_1, "false");
								holder.selecColumnSubscription.setBackgroundResource(R.drawable.infox_orange_frame);
								holder.selecColumnSubscription.setTextColor(Color.parseColor("#fa6400"));
								holder.selecColumnSubscription.setText("订阅");
							} else {
								holder.selecColumnSubscription.setTag(R.id.tag_1, "true");
								holder.selecColumnSubscription.setBackgroundResource(R.drawable.infox_searchitem_third);
								holder.selecColumnSubscription.setTextColor(Color.parseColor("#ffffff"));
								holder.selecColumnSubscription.setText("已订阅");
							}
						}
					}

					@Override
					public void handleFailure(Exception e, String msg) {
					}
				}, context);
			}
		});
		return convertView;
	}

	class ViewHolder {
		ImageView selecColumnImg;
		TextView selecColumnTitle;
		TextView selecColumnIntroduction;
		TextView selecColumnMessageCount;
		TextView selecColumnSubscription;
	}

}